package com.cjm.algorithm.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ReverseVowels345 {

    public static String reverseVowels1(String s) {
        int left = -1;
        int right = -1;
        int i =0 ;
        int j = s.length() - 1;
        char[] chars = s.toCharArray();
        while (i < j) {
            if (left != -1 && right != -1) {
                char temp = chars[left];
                chars[left] = chars[right];
                chars[right] = temp;
                left = -1;
                right = -1;
                i++;
                j--;
            }
            if (isWant(chars[i])) {
                left = i;
            } else {
                i ++;
            }

            if (isWant(chars[j])) {
                right = j;
            } else {
                j --;
            }
        }
        return new String(chars);
    }

    public static String reverseVowels(String s) {
        if (s.length() < 1) {
            return s;
        }
        Object[] result = want(s);
        if (result.length < 2) {
            return s;
        }
        int i = 0;
        int j = result.length - 1;
        char[] chars = s.toCharArray();
        while (i<=j) {
            int indexI = Integer.parseInt(String.valueOf(result[i]));
            int indexJ = Integer.parseInt(String.valueOf(result[j]));

            char aChar = chars[indexI];
            chars[indexI] = chars[indexJ];
            chars[indexJ] = aChar;

            i++;
            j--;
        }
        return new String(chars);
    }

    private static boolean isWant(char s) {
        List<String> l1 = Arrays.asList("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
        return l1.contains(String.valueOf(s));
    }

    private static Object[] want(String s) {
        List<String> l1 = Arrays.asList("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
        List<Integer> rs = new ArrayList<Integer>();
        int length = s.length();
        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            if (l1.contains(String.valueOf(c))) {
                rs.add(i);
            }
        }
        return rs.toArray();
    }

    public static void main(String[] args) {
        System.err.println(reverseVowels1("leetcode"));
    }
}
